﻿using System.Collections;
using System.Collections.Generic;
using System;

namespace BullsEyeGame
{
    /// <summary>
    /// This class will hold all the game data
    /// </summary>
    internal class GameData
    {
        private ArrayList m_GuessResultArrayList = null;
        private Guess m_TargetGuess = null;
        private int m_MaxNumberOfGuessesChosen; 

        private GameData(int i_MaxNumberOfGuessesChosen)
        {
            m_MaxNumberOfGuessesChosen = i_MaxNumberOfGuessesChosen;
            m_GuessResultArrayList = new ArrayList(i_MaxNumberOfGuessesChosen);
        	generateTargetGuess();
        }

        /// <summary>
        /// A static method to allow creation of this object
        /// </summary>
        /// <param name="i_MaxNumberOfGuessesChosen">The number of guesses selected by the user</param>
        /// <returns>The created GameDate</returns>
        public static GameData CreateGameData(int i_MaxNumberOfGuessesChosen)
        {
            GameData newGameData = null;

            if (GameLogics.IsValidNumberOfGuesses(i_MaxNumberOfGuessesChosen))
            {
                newGameData = new GameData(i_MaxNumberOfGuessesChosen);
            }

            return newGameData;
        }
                
        /// <summary>
        /// A property to get the all guess result of the game
        /// </summary>
        public ArrayList GuessesResults
        {
            get { return m_GuessResultArrayList; }
        }

        /// <summary>
        /// A property to get the target guess
        /// </summary>
        public Guess TagetGuess
        {
            get { return m_TargetGuess; }
        }

        /// <summary>
        /// A property to get the number of Guesses chosen by the user
        /// </summary>
        public int MaxNumberOfCuessesChosen
        {
            get { return m_MaxNumberOfGuessesChosen; }
            set { m_MaxNumberOfGuessesChosen = value; }
        }

        /// <summary>
        /// Will create a GuessResult from the input, add the result to its local GueastResult array
        /// </summary>
        /// <param name="i_NewGuess">A User Guess. 
        /// <remarks>
        /// If the number of Guesses submitted by the users is equal to the number of Guesses
        /// the user selected to have in this game then the guess will not be added and a ArgumentOutOfRangeException
        /// will be thrown
        /// </remarks>
        /// </param>
        /// <returns>The GuessResult for this input</returns>
        public GuessResult AddNewGuess(Guess i_NewGuess)
        {   
            GuessResult newGuessResult = null;

            if (m_GuessResultArrayList.Count < m_MaxNumberOfGuessesChosen)
            {
                newGuessResult = GuessResult.CreateGuessResult(i_NewGuess);

                if (newGuessResult != null)
                {
                    m_GuessResultArrayList.Add(newGuessResult);
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("cant add more guesses");
            }

            return newGuessResult;
        }

        private void generateTargetGuess()
        {
            System.Random randomNumber = new System.Random();
            List<Guess.eGuessLetters> targetGuessLetters = new List<Guess.eGuessLetters>(Guess.GuessLength);
            int currentGeneratedNumber;

            for (int i = 0; i < Guess.GuessLength; ++i)
            {
                currentGeneratedNumber = randomNumber.Next(Guess.NumberOfGuessLetters - 1);
                targetGuessLetters.Add((Guess.eGuessLetters)currentGeneratedNumber);
            }

            m_TargetGuess = Guess.CreateGuess(targetGuessLetters);
        }        
    }
}